package com.wmz;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@MapperScan(basePackages = {"com.wmz.mapper"})
@SpringBootApplication
public class DlykServerApplication implements CommandLineRunner {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    public static void main(String[] args) {
        SpringApplication.run(DlykServerApplication.class, args);
    }

    /**
     * springboot启动后，把redisTemplate这个Bean修改一下，修改key和value的序列化方法
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        //设置key的序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //对象映射工具，Java对象 和 Json对象进行转化
        ObjectMapper objectMapper = new ObjectMapper();
        //设置可见性
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        //激活类型
        objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(),ObjectMapper.DefaultTyping.EVERYTHING);

        //设置value的序列化
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(objectMapper,Object.class));

        //设置hashKey序列化
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        //设置hashValue序列化
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(objectMapper,Object.class));
    }
}
